package com.magic.himalaya.presenters;

import android.util.Log;

import androidx.annotation.Nullable;

import com.magic.himalaya.interfaces.ISearchPresenter;
import com.magic.himalaya.interfaces.ISearchViewCallBack;
import com.magic.himalaya.utils.Constants;
import com.magic.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.AlbumResult;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @create: Create by Magic on 2022/1/27.
 */
public class SearchPresenter implements ISearchPresenter {

    private static final String TAG = SearchPresenter.class.getSimpleName();

    private List<ISearchViewCallBack> mISearchViewCallBacks = new ArrayList<>();

    private static SearchPresenter mSearchPresenter = null;//本类实例
    private int mPageNum = 1;//搜索的页数
    private List<Album> mAlbums;

    private String mKeybord;//临时存储的搜索词

    private SearchPresenter() {

    }

    public static SearchPresenter getInstance() {
        if (mSearchPresenter == null) {
            synchronized (SearchPresenter.class) {
                if (mSearchPresenter == null) {
                    mSearchPresenter = new SearchPresenter();
                }
            }
        }
        return mSearchPresenter;
    }

    /**
     * 请求搜索的关键词数据
     *
     * @param str
     */
    public void requstData(String str, IDataCallBack<SearchAlbumList> searchAlbumListIDataCallBack) {
        String q = str;
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.SEARCH_KEY, q);
        map.put(DTransferConstants.CATEGORY_ID, 0 + "");
        map.put(DTransferConstants.PAGE, mPageNum + "");
        CommonRequest.getSearchedAlbums(map, searchAlbumListIDataCallBack);
    }

    /**
     * 请求热词数据
     *
     * @param dataCallBack
     */
    private void requstHotWord(IDataCallBack<HotWordList> dataCallBack) {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.TOP, Constants.HotTopCount + "");
        CommonRequest.getHotWords(map, dataCallBack);
    }

    /**
     * 请求关键词的联想词数据
     *
     * @param keyword
     * @param dataCallBack
     */
    private void requstSuggestWord(String keyword, IDataCallBack<SuggestWords> dataCallBack) {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.SEARCH_KEY, keyword);
        CommonRequest.getSuggestWord(map, dataCallBack);
    }

    // =================================================== 以下为业务层实现方法

    /**
     * 重新搜索
     */
    @Override
    public void getReSearchContent() {
        mPageNum = 1;

    }

    /**
     * 搜索输入框的内容
     *
     * @param keyword 关键字
     */
    @Override
    public void getSearchContent(String keyword) {
        mPageNum = 1;
        requstData(keyword, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(@Nullable SearchAlbumList searchAlbumList) {
                mKeybord = keyword;
                mAlbums = searchAlbumList.getAlbums();
                for (ISearchViewCallBack callBack : mISearchViewCallBacks) {
                    callBack.onSearchResultLoaded(mAlbums);
                }
                LogUtil.d(TAG, mAlbums.size() + "::::搜索结果");

            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    /**
     * 获取热词
     */
    @Override
    public void getHotWord() {
        requstHotWord(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(@Nullable HotWordList hotWordList) {
                List<HotWord> hotWords = hotWordList.getHotWordList();

                for (HotWord hotWord : hotWords) {
                    LogUtil.d(TAG, hotWords.size() + "::::::热词数量+"+hotWord.getSearchword());
                }
                for (ISearchViewCallBack callBack : mISearchViewCallBacks) {
                    callBack.onHotwordLoaded(hotWords);
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG, "code ---- >>" + i + ",,message ---- >>" + s);
            }
        });
    }

    /**
     * 加载更多
     */
    @Override
    public void loadMore() {
        mPageNum += 1;
        requstData(mKeybord, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(@Nullable SearchAlbumList searchAlbumList) {
                List<Album> albums = searchAlbumList.getAlbums();
                if (albums.size()!=0) {
                    //有数据时
                    for (Album album : albums) {
                        if (!mAlbums.contains(album)) {
                            mAlbums.add(album);
                        }
                    }
                    mOnfinishLoadMore.setData(null);
                    for (ISearchViewCallBack callBack : mISearchViewCallBacks) {
                        callBack.onSearchResultLoaded(mAlbums);
                    }
                    LogUtil.d(TAG, mAlbums.size() + "::::搜索结果");
                }else{
                    //空数据时
                    if (mOnfinishLoadMore != null)
                        mOnfinishLoadMore.setData("没有更多数据了!");
                }
            }

            @Override
            public void onError(int i, String s) {
                if (mOnfinishLoadMore!=null)
                    mOnfinishLoadMore.setData("加载错误!");
            }
        });

    }

    /**
     * 联想词
     *
     * @param keyword
     */
    @Override
    public void getRecommendWord(String keyword) {
        if (keyword != null) {
            requstSuggestWord(keyword, new IDataCallBack<SuggestWords>() {
                @Override
                public void onSuccess(@Nullable SuggestWords suggestWords) {
                    List<QueryResult> keyWordList = suggestWords.getKeyWordList();
                    for (ISearchViewCallBack callBack : mISearchViewCallBacks) {
                        callBack.onRecommendSuggestWords(keyWordList);
                    }
                    LogUtil.d(TAG, keyWordList.size() + "::::::联想词数量");
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        }
    }

    @Override
    public void registerViewCallBack(ISearchViewCallBack iSearchViewCallBack) {
        if (!mISearchViewCallBacks.contains(iSearchViewCallBack)) {
            mISearchViewCallBacks.add(iSearchViewCallBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(ISearchViewCallBack iSearchViewCallBack) {
        mISearchViewCallBacks.remove(iSearchViewCallBack);
    }


    private OnfinishLoadMore mOnfinishLoadMore = null;

    public void setOnfinishLoadMore(OnfinishLoadMore onfinishLoadMore) {
        mOnfinishLoadMore = onfinishLoadMore;
    }

    public interface OnfinishLoadMore{
        void setData(String str);
    }
}
